import 'package:app/services/address.dart';
import 'package:app/services/dio_manager.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';

import 'leave_model.dart';
import 'leave_top_dialog_page.dart';

class LeaveController extends GetxController{

  bool isTopShow = false;
  String replyType = '全部';
  var replyId = '';



  TextEditingController editingController = TextEditingController();

  EasyRefreshController easyRefreshController = EasyRefreshController();
  List dataArr = [];

  int page = 1;

  requestData({var search})async{

    var params = {
      // 'search':'Appointment',
      'search':search,
      'state':replyId, //confirm-待审批，refuse-已拒绝，validate-已同意，draft-草稿
      'my_approve':'0',
      'page':page,
      'limit':10,
    };
    var json = await DioManager().kkRequest(Address.hrLeavePage,params:params,method: 'get');
    LeaveModel model = LeaveModel.fromJson(json);
    if(page == 1){
      easyRefreshController.resetLoadState();
      dataArr.clear();
    }
    if(model.data!=null){
      dataArr.addAll(model.data!);
    }else if(model.data == null){
      BotToast.showText(text: '暂无更多');
      easyRefreshController.finishLoad(noMore: true);
    }
    update();
  }


  onRefresh()async{
    page = 1;
    requestData(search: editingController.text);
  }

  onLoad()async{
    page++;
    requestData(search: editingController.text);
  }
  /// 点击的弹框
  topClick() async {
    isTopShow = true;

    print(replyType);
    var a;
    if (replyType == '全部') {
      a = 0;
    }
    if (replyType == '待审批') {
      a = 1;
    }
    if (replyType == '已拒绝') {
      a = 2;
    }
    if (replyType == '已同意') {
      a = 3;
    }

    var data =
    await Get.dialog(LeaveTopDialogPage(a), barrierColor: Colors.transparent);
    if (data != null) {
      print(data);
      isTopShow = data['isShow'];
      replyType = data['text'];
      replyId = data['reply_type'];
      page = 1;
      requestData();
    }
    update();
  }


  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    requestData();
  }

}